package cn.itsource.org.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.MD5;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.query.ShopQuery;
import cn.itsource.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 店铺控制器
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "店铺管理", description = "店铺操作接口")
public class ShopController {

    public static void main(String[] args) {
        String s1 = IdUtil.fastSimpleUUID();
        System.out.println(s1);

        String s = MD5.create().setSalt(s1.getBytes()).digestHex("123");

        System.out.println(s);//
        //有salt
    }


    @Autowired
    private IShopService shopService;

    /**
     * 查询所有
     */
    @GetMapping("/activation/{uuid}")
    @ApiOperation(value = "店铺激活", notes="")
    public AjaxResult activation(@PathVariable("uuid") String uuid){
        //查询分页数据
        return shopService.activation(uuid);
    }
    /**
     * 查询所有
     */
    @PostMapping("/settlement")
    @ApiOperation(value = "店铺入驻", notes="")
    public AjaxResult settlement(@RequestBody @Valid Shop shop){
        //查询分页数据
        return shopService.settlement(shop);
    }

    /**
     * 查询所有
     */
    @GetMapping
    @ApiOperation(value = "查询所有店铺信息", notes="查询所有店铺信息，get请求")
    public List<Shop> findAll(){
        return shopService.findAll();
    }

    /**
     * 查询所有
     */
    @PostMapping("/findPage")
    @ApiOperation(value = "查询部门分页数据", notes="查询数据")
    public PageList<Shop> findPage(@RequestBody ShopQuery shopQuery){
        //查询分页数据
        return shopService.findPage(shopQuery);
    }

    /**
     * 新增或修改的操作
     * 根据id来区分新增和修改
     * 怎么区分请求使用的请求方式  这里是post
     */
    @PostMapping
    public AjaxResult saveOrUpdate(@RequestBody @Valid Shop shop){

        //对所有的入参进行一个有效判断
        /*Optional.ofNullable(shop.getName()).orElseThrow(() -> new RuntimeException("部门名称不能为空"));

        if (StringUtils.isBlank(shop.getSn())){
            throw  new RuntimeException("部门编号不能为空");
        }
*/
        try {
            if (shop.getId() == null){
                //没有id就是新增
                shopService.save(shop);
            }else{
                //修改
                shopService.update(shop);
            }
            //返回成功对象
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败对象
            return AjaxResult.createError("操作异常,向日葵！");
        }
    }
    /**
     * 删除操作
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable("id") Long id){

        try {
            //删除
            shopService.delete(id);
            //返回成功对象
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败对象
            return AjaxResult.createError("操作异常,向日葵！");
        }
    }
    /**
     * 批量删除操作
     */
    @PostMapping("/batchDelete")
    public AjaxResult delete(@RequestBody ShopQuery shopQuery){

        try {
            //批量删除操作
            shopService.batchDelete(shopQuery);
            //返回成功对象
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败对象
            return AjaxResult.createError("操作异常,向日葵！");
        }
    }


   

}
